<template>
  <el-dialog
    :visible.sync="dialogVisible"
    width="605px"
    :show-close="false"
    :close-on-click-modal="false"
  >
    <div slot="title">
      <span class="el-icon-warning" style />
      <span class="titleTxt">{{ titleTxt }}</span>
    </div>
    <!-- <span class="content">{{msgContent}}</span> -->
    <div class="content">
      <!-- <span v-if="!isSetTask" class="msgTitle">FI、FI00账户持仓量提醒：</span> -->
      <h4 v-if="!isSetTask" class="msgTitle">FI、FI00账户持仓量提醒：</h4>
      <span v-if="!isSetTask" class="msgCont">{{ hold }}</span>

      <!-- <br /> -->
      <!-- <span v-if="!isSetTask" class="msgTitle">FI、FI00账户债券提醒：</span> -->
      <h4 v-if="!isSetTask" class="msgTitle">FI、FI00账户债券提醒：</h4>
      <span v-if="!isSetTask" class="msgCont">{{
        bondStr ? bondStr : "无"
      }}</span>

      <!-- <br /> -->
      <!-- <span class="msgTitle">BU00、BU01账户持仓量提醒：</span> -->
      <h4 class="msgTitle">BU00、BU01账户持仓量提醒：</h4>
      <span class="msgCont">{{
        buNum
          ? `BU00和BU01截止${this.currentHour}持仓为${buNum}亿元。其中BU00持仓为${bu00Num}亿元，包含${BU00Str}。BU01持仓为${bu01Num}亿元,包含${BU01Str}。`
          : `BU00和BU01截止${this.currentHour}持仓为0`
      }}</span>

      <!-- <br/> -->
      <!-- <span class="msgTitle">BU02当日持仓分析提醒：</span> -->
      <h4 class="msgTitle">BU02当日持仓分析提醒：</h4>
      <span class="msgCont">{{
        BU02Str === ""
          ? `BU02截止${this.currentHour}当前未有持仓超过7天的债券`
          : `BU02截止${this.currentHour}，${BU02Str}`
      }}</span>
      <!-- <br/>
      <span>{{`其中BU00持仓为${bu00Num}，包含${BU00Str}`}}</span>
      <br/>
      <span>{{`BU01截止${this.currentHour}持仓为${bu01Num},包含${BU01Str}`}}</span>-->
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button size="small" type="primary" @click="read">已阅</el-button>
    </div>
  </el-dialog>
</template>

<script>
import store from "@/store";
import moment from "moment";
import {
  getHomeFi,
  saveHomeMessage,
  bu00Bu01Waring,
  bu02Waring,
} from "@/api/sys/message.js";
import { setTimeout } from "timers";
import { getToken } from "@/utils/auth";
export default {
  name: "",
  components: {},
  props: {},
  data() {
    return {
      dialogVisible: false,
      titleTxt: "FI、FI00账户/BU00、BU01账户持仓监控/BU02当日持仓分析",
      msgContent: "",
      bondStr: "",
      hold: "",
      buNum: 0,
      bu00Num: 0,
      bu01Num: 0,
      BU00Str: "",
      BU01Str: "",
      BU00Date: "",
      isSetTask: false, // 是否六点自动弹出
      currentDate: moment().format("MM月DD日"),
      currentHour: moment().format("MM月DD日hh点"),
      BU02Str: "",
      BU02Date: "",
    };
  },
  computed: {},
  watch: {},
  created() {
    this.getData();
  },
  mounted() {},
  methods: {
    getData() {
      if (!getToken()) return;
      if (sessionStorage.getItem("isRead") == 1) return;
      getHomeFi().then((res) => {
        const obj = res[0];
        this.bondStr = "";
        this.hold = "";
        for (const k in obj) {
          if (
            k == "tatal_settled_qtty" ||
            k == "fi_settled_qtty" ||
            k == "fi00_settled_qtty" ||
            k == "FI账簿与FI00账簿总持仓" ||
            k == "FI00账簿单只债券持仓时间(工作日)" ||
            k == "FI账簿单只债券持仓时间(工作日)" ||
            k == "BU02账簿单只债券持仓时间(自然日)"
          ) {
            const num = (
              obj.tatal_settled_qtty - obj.FI账簿与FI00账簿总持仓
            ).toFixed(2);
            this.hold = `截止${this.currentDate}FI持仓余额为${
              obj.fi_settled_qtty ? obj.fi_settled_qtty : 0
            }亿元，FI00持仓余额为${
              obj.fi00_settled_qtty ? obj.fi00_settled_qtty : 0
            }亿元，合计${
              obj.tatal_settled_qtty ? obj.tatal_settled_qtty : 0
            }亿元，${
              num > 0
                ? "已超过" +
                  obj.FI账簿与FI00账簿总持仓 +
                  "亿元阈值限额," +
                  "已超出阈值限额" +
                  num +
                  "亿元。"
                : "未超出阈值限额。"
            }`;
          } else {
            this.bondStr += `${k}，已持仓超过${obj[k]}天。`;
          }
        }
        this.msgContent = this.hold + "/" + this.bondStr;
        this.getBu01();
      });
    },
    getBu01() {
      this.buNum = 0;
      this.bu00Num = 0;
      this.bu01Num = 0;
      this.BU00Str = "";
      this.BU01Str = "";
      bu00Bu01Waring().then((res) => {
        if (res.results) {
          this.BU00Date = res.results[0]
            ? res.results[0].dataDate
            : moment().format("yyyyMMDD");
          let str = "";
          let str1 = "";
          res.results.forEach((item) => {
            this.buNum += item.settledqtty;
            if (item.book == "BU01") {
              this.bu01Num += item.settledqtty;
              str += `${item.name == null ? "" : item.name}，(债券ID${
                item.secid
              })；`;
              this.BU01Str = str.slice(0, -1);
            } else {
              str1 += `${item.name == null ? "" : item.name}，(债券ID${
                item.secid
              })；`;
              this.BU00Str = str1.slice(0, -1);
              this.bu00Num += item.settledqtty;
            }
          });

          this.getBu02();
        }
      });
    },
    getBu02() {
      this.BU02Str = "";
      bu02Waring().then((res) => {
        if (res) {
          this.BU02Date = res[0] ? res[0].TXN_DT : moment().format("yyyyMMDD");
          res.forEach((item) => {
            this.BU02Str += `${item.SECID == null ? "" : item.SECID}，（${
              item.NAME == null ? "" : item.NAME
            }）已持仓超过${
              item.DURATION_CALENDAR_DAYS == null
                ? "0"
                : item.DURATION_CALENDAR_DAYS
            }天；`;
          });
          this.dialogVisible = true;
          this.setTask(18, 20);
        } else if (res) {
          this.dialogVisible = true;
          this.setTask(18, 20);
        }
      });
    },
    read() {
      const paramsArr = [];
      const BU00params = {
        message: this.buNum
          ? `BU00和BU01截止${this.currentHour}持仓为${this.buNum}亿元。其中BU00持仓为${this.bu00Num}亿元，包含${this.BU00Str}。BU01持仓为${this.bu01Num}亿元,包含${this.BU01Str}。`
          : `BU00和BU01截止${this.currentHour}持仓为0`,
        date: this.BU00Date,
        book: "BU",
      };
      const BU02params = {
        message: this.BU02Str,
        date: this.BU02Date,
        book: "BU02",
      };
      paramsArr.push(BU00params);
      paramsArr.push(BU02params);

      if (!this.isSetTask) {
        const params = {
          message: this.msgContent,
          date: moment().format("yyyyMMDD"),
          book: "FI",
        };
        paramsArr.push(params);
      }

      const str = JSON.stringify(paramsArr);
      const str1 = str.substring(1, str.length - 1);
      saveHomeMessage({ messageResult: str1 }).then((res) => {
        if (res.success) {
          this.$message.success(res.msg);
          this.dialogVisible = false;
          sessionStorage.setItem("isRead", 1);
        } else {
          this.$message.error(res.msg);
        }
      });
    },
    // 定时任务提醒
    setTask(setHour, setMinutes) {
      const _this = this;
      var timerTime, nowTime, nowS, setTimeS;
      nowTime = new Date();
      nowS =
        nowTime.getHours() * 3600 +
        nowTime.getMinutes() * 60 +
        nowTime.getSeconds();
      setTimeS = setHour * 3600 + setMinutes * 60;
      timerTime =
        setTimeS > nowS ? setTimeS - nowS : setTimeS + 24 * 3600 - nowS;
      setTimeout(() => {
        sessionStorage.setItem("isRead", 0);
        _this.titleTxt = "BU00、BU01账户持仓监控";
        _this.isSetTask = true;
        (_this.currentDate = moment().format("MM月DD日")),
          (_this.currentHour = moment().format("MM月DD日hh点"));
        _this.getBu01();
        _this.getBu02();
      }, timerTime * 1000);
    },
  },
};
</script>

<style scoped lang="scss">
// @import "./msg.scss";
.el-dialog {
  .el-dialog__body {
    flex: none !important;
  }
  .el-icon-warning {
    font-size: 21px;
    color: #579bff;
    margin-right: 3px;
  }
  .titleTxt {
    color: rgba(255, 255, 255, 0.65);
    font-size: 18px;
  }
  .msgTitle {
    font-size: 15px;
    margin-top: 8px;
    margin-bottom: 5px;
    padding-bottom: 5px;
    border-bottom: 1px solid #5d7590;
    // color: #000;
  }
  .dialog-footer {
    // display: inline-block;
    text-align: center !important;
    padding: 5px;
  }
  .content {
    max-height: 400px;
    overflow-y: auto;
    padding: 0 20px 0 45px;
    color: rgba(255, 255, 255, 0.65);
  }
  .borderBom {
    height: 1px;
    background-color: #5d7590;
    margin-top: 5px;
    margin-bottom: 5px;
  }
  .msgCont {
    // display: inline-block;
    // padding-top: 8px;
  }
}
</style>
